#coding=utf-8


from DjangoBbs.BbsTheme import models

__author__ = 'czq'
def Theme_Filter_Rank(request,bbsClass=None,isSearch=False,isUser=False):
    list=models.BbsTheme.objects_show.all()#classId__visitPerm=None
    if bbsClass==None:
        pass
    else:
        list = list.filter(classId=bbsClass)
    if True:
            rank=request.GET.get('rank','-addTime')
            if rank=='':
                rank='-addTime'
            order_type=request.GET.get('order_type','1')
            filter=request.GET.get('filter','')
            if isSearch:
                list=list.filter(classId__visitPerm=None)
                value=request.GET.get('value','')
                if value!='':
                        from django.db.models import Q
                        list=list.filter(Q(title__contains=value) | Q(content__contains=value) )


                uid=request.GET.get('uid','')
                if uid!='':
                    from django.contrib.auth.models import User
                    user=User.objects.get(id=uid)
                    list=list.filter(addUser=user)
                else:
                    if value=='':
                        list=list.filter(isTop=True)

            if filter!='':
                sw={
                    'isBoutique':lambda:list.filter(isBoutique=True),
                    'isHighlight':lambda :list.filter(isHighlight=True),
                    'isClose':lambda :list.filter(isClose=True),
                    'isLock':lambda :list.filter(isLock=True),
                    'isVote':lambda :list.filter(isVote=True),
                }
                list=sw[filter]()

            if order_type=='1':
                    list = list.order_by('-isTop',rank)
            else:
                    list = list.order_by('-isTop',rank).reverse()
    return list

